Skip to content

XUtils: add NULL pointer guard in String_contains_i - #2152

Closed
EswaraValli wants to merge 1 commit into
htop-dev:mainfrom
EswaraValli:fix-1864-string-contains-null-check
Closed

EswaraValli wants to merge 1 commit into
htop-dev:mainfrom
EswaraValli:fix-1864-string-contains-null-check

Conversation

@EswaraValli

Copy link
Copy Markdown

Summary

Fixes: #1864

In issue #1864, a segmentation fault (Signal 11) occurred on macOS during process table filtering:

strcasestr_l + 132
String_contains_i + 156
Process_rowMatchesFilter + 108
Table_rebuildPanel + 372
ScreenManager_run + ...

While the exact user action that activated the filter in the reported incident is unknown (e.g. interactive F4 filter entry vs. -F command-line flag), the backtrace establishes that an incremental filter was active when the crash occurred (Process_rowMatchesFilter only invokes String_contains_i when table->incFilter is non-NULL).

On Darwin/macOS (such as unnamed threads or unpopulated comm entries) and Linux (kernel threads and zombies), Process_getCommand() can legitimately return NULL. String_contains_i() previously passed s1 directly to strcasestr(), causing an immediate crash when encountering a NULL command.

Fix

Add an early guard at the beginning of String_contains_i():

if (!s1 || !s2)
   return false;

If either the haystack or needle is NULL, the function safely returns false. In Process_matchesFilter(), !String_contains_i(...) evaluates to true, correctly filtering out the entry without crashing.

Verification

  • Verified against the compiled XUtils.o across 9 regression test cases in an external test harness covering NULL haystacks, NULL needles, both pointers NULL, single/multi-needle matching, and multi-needle NULL permutations (unpatched failed 5/9 cases with SIGSEGV; patched passed 9/9).
  • Verified full call path against compiled Process.o + XUtils.o with mock unnamed thread entries (proc.cmdline = NULL), confirming graceful filtering without crashing.
  • Verified git diff --check, ./check-pcp-style.sh, and make check pass with zero warnings or errors.

On Darwin/macOS and Linux, Process_getCommand() can
legitimately return NULL for certain processes or threads.
When an incremental filter is active, this NULL pointer
can reach String_contains_i(), which previously passed it
to strcasestr() and caused a segmentation fault.

Add an early NULL guard to prevent the crash.

Verified with external regression harnesses covering
NULL inputs and normal single- and multi-needle matching.

Fixes: htop-dev#1864
Assisted-by: Antigravity (Google DeepMind)
Signed-off-by: Eswara Valli <killadaeswaravalli@gmail.com>
@coderabbitai

coderabbitai Bot commented Nov 11, 2026

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

String_contains_i now returns false when either input string is NULL. Its existing search behavior remains unchanged for non-NULL inputs.

Assessment against linked issues:

Objective Addressed Explanation
Prevent the segmentation fault reported in issue #1864 ❓ The report points to String_contains_i, but it does not establish that either input was NULL.

Suggested reviewers: benbe

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to 95f00

This change prevents a crash when a process has no command during filtering. No merge-blocking risk was found.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Two strings approach the search gate
A NULL turns back before its fate
The steady terms still seek their match
While careful checks keep watch on each
The search goes on when both are whole

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 641250ac-9ab8-4756-a65c-63d9a928032d
Commits

Reviewing files that changed from the base of the PR and between 5091900 and 95f005f.

Files selected for processing: 1

  • XUtils.c

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@fasterit

Copy link
Copy Markdown
Member

Thanks for bringing this up to the top of the stack again.
Your Google Deepmind just applied the fix from #1864 (comment) , so I'll merge that and save us the AI blurb in the commit.

@fasterit fasterit closed this in 6655b88 Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segmentation fault 11

2 participants